/**
 * Created by zuojianshijue on 17/3/2.
 */
var config = {};

config.Route = 0;
config.interfaceIpAry = ['http://172.16.103.145:9002', ''];
config.interfaceUrl = config.interfaceIpAry[0];

config.interface = {};//接口集合

config.interface.testInterface = config.interfaceUrl + '/single/stock/2367232001/102345?shopSid=';

//商品搜索列表
config.interface.getShopList = config.interfaceUrl + '/search';

//用户关注品牌列表
config.interface.getLikesBrandList = config.interfaceUrl + '/favorite/brand';
//热门品牌列表
config.interface.getHotBrandList = config.interfaceUrl + '/ssdservice/web/displayList';
//本店推荐品牌列表
config.interface.getStoreBrandList = config.interfaceUrl + '/shop/brands/';

//关注品牌
config.interface.likesBrand = config.interfaceUrl + '/favorite/brand/addFavoriteBrand';
//取消关注品牌
config.interface.unLikesBrand = config.interfaceUrl + '/favorite/brand/deleteFavoriteBrand';
//按照品牌ID查询商品
config.interface.getShopListbyBrandID = config.interfaceUrl + '/brand/products';
//商品搜索

config.shopInfoUrl = config.interfaceUrl + '/h5/v1_0_0/html/goodsDetail.html';

/*
 http://app.shopin.cn/index.html
 http://app.shopin.cn/index_wm.html
 http://app.shopin.cn/index_kid.html
 */


var tools = {};
config.ShopInfo = [
    {"shopId": "1001", "shopName": "王府井店"},
    {"shopId": "1002", "shopName": "亚运R村店"},
    {"shopId": "1004", "shopName": "五棵松店"},
    {"shopId": "1005", "shopName": "中关村店"},
    {"shopId": "1006", "shopName": "朝阳门店"},
    {"shopId": "1007", "shopName": "三里河店"},
    {"shopId": "1008", "shopName": "来广营店"},
    {"shopId": "1010", "shopName": "回龙观店"},
    {"shopId": "1011", "shopName": "草桥店"}
];

config.imgUrl = 'http://images.shopin.net/images';

config.memberSid = '16753144';

tools.doAjax = function (objParams, interfaceName, successCallback, errorCallback, completeCallback) {
    var $_body = $('body');
    var data = objParams;
    if (!$_body.hasClass('ajaxing')) {
        $_body.addClass('ajaxing');
        $.ajax({
            url: interfaceName,
            type: 'post',
            contentType: "application/json",
            dataType: 'json',
            timeout: 6000,//6000
            data: data,
            success: function (data) {
                successCallback(data);
            },
            error: function () {
                tools.isFunction(errorCallback) ? errorCallback() : '';
            },
            complete: function () {
                tools.isFunction(completeCallback) ? completeCallback() : '';
                $_body.removeClass('ajaxing');
            }
        });
    }
};

tools.doAjaxJSONP = function (objParams, interfaceName, successCallback, errorCallback, completeCallback) {
    var $_body = $('body');
    var data = objParams;
    if (!$_body.hasClass('ajaxing')) {
        $_body.addClass('ajaxing');
        $.ajax({
            url: interfaceName,
            type: 'get',
            dataType: 'jsonp',
            timeout: 6000,//6000
            data: data,
            success: function (data) {
                successCallback(data);
            },
            error: function () {
                tools.isFunction(errorCallback) ? errorCallback() : '';
            },
            complete: function () {
                tools.isFunction(completeCallback) ? completeCallback() : '';
                $_body.removeClass('ajaxing');
            }
        });
    }
};

//判断是否是function
tools.isFunction = function (fn) {
    return Object.prototype.toString.call(fn) === '[object Function]';
}


tools.parseUrlParams = function (url) {
    var reg = /([^?=&]+)=([^?=&]+)/g;
    var paramsObj = {};
    if (url) {
        while (reg.test(url)) {
            paramsObj[RegExp.$1] = RegExp.$2;
        }
    }
    return paramsObj;
};


tools.parseQueryString = function (url) {
    var reg_url = /^[^\?]+\?([\w\W]+)$/,
        reg_para = /([^&=]+)=([\w\W]*?)(&|$|#)/g,
        arr_url = reg_url.exec(url),
        ret = {};
    if (arr_url && arr_url[1]) {
        var str_para = arr_url[1], result;
        while ((result = reg_para.exec(str_para)) != null) {
            ret[result[1]] = result[2];
        }
    }
    return ret;
};


var otherTools={};//其他例外需求公用方法

otherTools.getShopList=function (ajaxData) {
    //var self_ = this;
    //var ajaxData = {"pageNum": self_.pageNum, "pageSize": self_.pageSize, "sortType": self_.sortType};
    tools.doAjax(JSON.stringify(ajaxData), config.interface.getShopList, function (data) {
        var shopListObj = [];
        $(data.data.esProducts).each(function (i, v) {
            shopListObj.push('<div class="si-block" gonative="');
            shopListObj.push(config.shopInfoUrl);
            shopListObj.push('?productSid=');
            shopListObj.push(v.productSid);
            shopListObj.push('&supplySid=');
            shopListObj.push(v.supplySid);
            shopListObj.push('"><img data-echo="');

            shopListObj.push(config.imgUrl);
            shopListObj.push(v.proPictDir);
            shopListObj.push(v.proPictName);


            shopListObj.push('" class="si-shopimg" src="');

            shopListObj.push(config.interfaceUrl);
            shopListObj.push('/cms/img/160x160.jpg');

            shopListObj.push('"><div class="si-shopname">');
            shopListObj.push(v.productName);
            shopListObj.push('</div><div class="si-shopp"><span>￥</span>');
            shopListObj.push(v.cuPrice.toFixed(2));
            shopListObj.push('</div></div>');

        });
        $('.shop-item').html(shopListObj.join(''));
        WebViewJsBridge.goNativeEvent();
        Echo.init({
            offset: 150,
            throttle: 0
        });
    });
}